<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\components;
use Yii;
/**
 * Description of BaseViewListBarChart
 *
 * @author shuai
 */
class BaseViewListBarChart extends \yii\base\Widget{
    /**
     * 主表对象
    */
    private $_Base=null;
    private $find;
      /**
    * 开始的时间戳
    */
   public $beginTime=0;
   /**
    * 结束的时间戳
   */
   public $endTime=0;
   
   private $_max=0;
   private $_min=0;


   /**
    * 分几段
   */
    public $SegmentNum=0;
    public function __construct($th) {
        $this->_Base=$th;
        $this->find=$this->_Base->find();
        $this->loadinit();
    }
    
    
    /**
     * 生成的时间数组
    */
   private $timeArray=[];
   /**
    * 时间间隔
   */
  private $timeInterval=0;
    /**
     * 初始化
    */
private function loadinit(){
    $daytime=60*60*24;
    $time=  strtotime(date("Y-m-d",time()))+$daytime;
    $chaday=15;
    
    $this->beginTime=$time-$chaday*$daytime;
    $this->endTime= $time; 
    $this->SegmentNum=$chaday;
    if(!empty($_GET['beginTime'])){
        $this->beginTime=strtotime($_GET['beginTime']);
    }
    if(!empty($_GET['endTime'])){
        $this->endTime=strtotime($_GET['endTime']);
    }
    if(!empty($_GET['SegmentNum'])){
        $this->SegmentNum=$_GET['SegmentNum'];
    }
    if($this->SegmentNum<=0){
        Fun::showmsg("参数错误,SegmentNum小于等于0");
    }
    
    $xiangcaTime=  $this->endTime-$this->beginTime;
    $this->timeInterval=$xiangcaTime/$this->SegmentNum;
    for($i=0;$i<$this->SegmentNum;$i++){
        $this->timeArray[$i]=$this->beginTime+($this->timeInterval*$i);
    }
}

      /**
     * 每种分类的数据
    */
   private $_classData=[];
    /**
     * 获取表格数据
    */
   public function get_data(){  
      /*
       foreach ($this->timeArray as $vo) {
           echo Fun::date($vo);echo "\r\n";
       }     */  
      // print_r($this->timeArray);exit;
       
       
       foreach ($this->_Base->BarChartCondition as $key=>$vo) {
           $list=$this->getClassList($vo);
           $this->_classData[$key.':'.array_sum($list)]=$list;
       }
       return $this->_classData;
   }
   
   
 


  /**
    * 获取每个分类的数据
   */
   private function getClassList($condition){
       $array=[];
       foreach ($this->timeArray as $vo) { 
                   $array[]=(int)$this->getCacheCount($vo, $vo+$this->timeInterval, $condition);
       }
       $max=  max($array);
       $min=min($array);
       $this->_max=max($this->_max,$max);
      $this->_min=min($this->_min,$min);
       return $array;
   }
   
   /**
    * 根据参数获取值
   */
  private function getCacheCount($begin,$end,$condition){
      $cacheName= md5($this->_Base->getName().$begin.$end.$condition);
      $isCache=false;
      if($end<time()){///如果结束的时间小于现在的时间,那么检查缓存是否存在该区间的值,如果有,直接返回
          $isCache=true;
          $cache=Yii::$app->cache->get($cacheName);
          
          if($cache!==false){              
              return $cache;
          }
      }
      $val=$this->find->
                   where("time >=:begin AND time < :end",
                   [':begin'=>$begin,':end'=>$end])->
                   andWhere($condition)->
                   count();
      if($isCache){
          Yii::$app->cache->set($cacheName, $val);
      }
      return $val;
  }

  

  /**
    * 获取网页显示的内容
   */
  public function getHtml(){
      $data=  $this->get_data();
      return $this->_Base->render("barchart",
              [
                  'data'=>$data,
                  'timeArray'=>  $this->timeArray,
                  'base'=>$this
              ]);
  }
  
  /**
   * 获取最大值
  */
 public function getMax(){
     return $this->_max;
 }
 public function getMin(){
     return $this->_min;
 }

 
 /**
   * 获取一个颜色值
  */
  public function color($i){
      $arr=[
          '#ee7951',
          '#4fb9f0',
          '#6EF04F',
          '#8D55E6',
          '#E9F718',
      ];
      return $arr[$i%(count($arr))];
  }
}
